package com.lanou.musicproject.adapter;

import android.app.Activity;
import android.content.Context;
import android.text.Layout;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.lanou.musicproject.R;
import com.lanou.musicproject.activity.MainActivity;
import com.lanou.musicproject.bean.MyMusicList;
import com.lanou.musicproject.bean.Rank;
import com.lanou.musicproject.bean.SongListRecomm;
import com.squareup.picasso.Picasso;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by dllo on 15/10/20.
 */
public class RankListViewAdapter extends BaseAdapter {
    private List<Rank.ContentEntity> data;
    private LayoutInflater inflater;
    private Rank.ContentEntity entity;
    private Context mContext;

    public RankListViewAdapter(Context c, List<Rank.ContentEntity> d) {
        inflater = LayoutInflater.from(c);
        mContext = c;
        data = d;
    }

    @Override
    public int getCount() {
        return data != null && data.size() > 0 ? data.size() : 0;
    }

    @Override
    public Object getItem(int position) {
        return data != null && data.size() > 0 ? data.get(position) : null;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;
        if (convertView == null) {
            convertView = inflater.inflate(R.layout.rank_item, parent, false);
            holder = new ViewHolder();
            holder.rank_Imag = (ImageView) convertView.findViewById(R.id.rank_Imag);
            holder.rank_Name = (TextView) convertView.findViewById(R.id.rank_Name);
            //歌曲初始化
            holder.rank_title = (TextView) convertView.findViewById(R.id.rank_title);
            holder.rank_title_O = (TextView) convertView.findViewById(R.id.rank_title_O);
            holder.rank_title_S = (TextView) convertView.findViewById(R.id.rank_title_S);
            //歌手初始化
            holder.rank_author = (TextView) convertView.findViewById(R.id.rank_author);
            holder.rank_author_O = (TextView) convertView.findViewById(R.id.rank_author_O);
            holder.rank_author_S = (TextView) convertView.findViewById(R.id.rank_author_S);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }


        //获取数据
        entity = (Rank.ContentEntity) getItem(position);
        if (entity != null) {
            //设置歌曲名
            holder.rank_title.setText("1. " + entity.getContent().get(0).getTitle());
            holder.rank_title_O.setText("2. " + entity.getContent().get(1).getTitle());
            holder.rank_title_S.setText("3. " + entity.getContent().get(2).getTitle());
            //设置歌手
            holder.rank_author.setText(entity.getContent().get(0).getAuthor());
            holder.rank_author_O.setText(entity.getContent().get(1).getAuthor());
            holder.rank_author_S.setText(entity.getContent().get(2).getAuthor());
            //获取排行榜名
            holder.rank_Name.setText(entity.getName());
            //获取排行榜图片
            String url = entity.getPic_s192();
            Picasso.with(mContext).load(url).into(holder.rank_Imag);
        }

        System.gc();

        return convertView;

    }

    private class ViewHolder {
        private ImageView rank_Imag;
        private TextView rank_Name, rank_title, rank_author, rank_title_O, rank_title_S, rank_author_O, rank_author_S;

    }

}
